﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class GroupPhotoDAL
    {
        private const string Waming = "SqlServerDAL.GroupPhotoDAL";

        public bool Save(GroupPhotoDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@GpName", SqlDbType.VarChar) {Value = dto.GpName},
                                         new SqlParameter("@GpUserId", SqlDbType.Int) {Value = dto.GpUserId},
                                         new SqlParameter("@GpAnnexId", SqlDbType.Int) {Value = dto.GpAnnexId},
                                         new SqlParameter("@GpTime", SqlDbType.DateTime) {Value = dto.GpTime},
                                         new SqlParameter("@GpAlbumId", SqlDbType.Int) {Value = dto.GpAlbumId},
                                         new SqlParameter("@GpGroupCode", SqlDbType.VarChar) {Value = dto.GpGroupCode},
                                         new SqlParameter("@GpSummary", SqlDbType.VarChar) {Value = dto.GpSummary}
                                     };
                if (dto.GpId == 0)
                {
                    sql =
                        @"insert into GroupPhoto(GpName,GpUserId,GpAnnexId,GpTime,GpAlbumId,GpGroupCode,GpSummary) values(
                        @GpName,@GpUserId,@GpAnnexId,@GpTime,@GpAlbumId,@GpGroupCode,@GpSummary)";
                }
                else
                {
                    sql =
                        @"update GroupPhoto set GpName=@GpName,GpUserId=@GpUserId,GpAnnexId=@GpAnnexId,GpTime=@GpTime,
                        GpAlbumId=@GpAlbumId,GpGroupCode=@GpGroupCode,GpSummary=@GpSummary where GpId=@GpId";
                    parameters.Add(new SqlParameter("@GpId", SqlDbType.Int) { Value = dto.GpId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(GroupPhotoDto dto)", ex);
                return false;
            }
        }
    }
}
